package com.cicoding.utlis;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;

public class RFIDCacheExample {

    /**
     * 初始化数据
     */

    public static Cache<String, Cache<String, Long>> RFIDCache = CacheBuilder.newBuilder()
            .maximumSize(6) // 外层缓存大小限制为6
            .build();


    public static void putToNestedCache(String outerKey, String innerKey, Long value) {
        // 获取内层缓存
        Cache<String, Long> innerCache = RFIDCache.getIfPresent(outerKey);
        if (innerCache == null) {
            // 如果内层缓存不存在，则创建并放入外层缓存
            innerCache = CacheBuilder.newBuilder().maximumSize(2000).build(); // 内层缓存大小限制为10
            RFIDCache.put(outerKey, innerCache);
        }
        // 向内层缓存放入数据
        innerCache.put(innerKey, value);
    }

    public static Long getFromNestedCache(String outerKey, String innerKey) {
        // 获取内层缓存
        Cache<String, Long> innerCache = RFIDCache.getIfPresent(outerKey);
        if (innerCache != null) {
            // 如果内层缓存存在，则从中获取数据
            return innerCache.getIfPresent(innerKey);
        }
        return null;
    }

    public static  Cache<String, Long>  getFromNestedCacheData(String outerKey) {
        // 获取内层缓存
        Cache<String, Long> innerCache = RFIDCache.getIfPresent(outerKey);
        return innerCache;
    }
}
